package com.zerowidth.db.op;

import com.google.gson.reflect.TypeToken;
import com.zerowidth.db.DBHelper;
import com.zerowidth.db.base.SqlResult;
import com.zerowidth.db.bean.MallBean;
import com.zerowidth.db.utils.IOUtils;
import com.zerowidth.moc.web.utils.GsonUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MallDB {

    public static List<MallBean> getMallList() {
        String sql = String.format("SELECT * FROM mall where enable = 1");

        SqlResult sr = DBHelper.executeQueryEx(sql);

        try {
            ResultSet rs = sr.rs;
            List<MallBean> mallBeanList = new ArrayList<>();
            while (rs.next()) {
                MallBean bean = new MallBean();
                bean.id = rs.getInt("id");
                bean.name = rs.getString("name");
                bean.cover =  GsonUtils.fromJsonStr(rs.getString("cover"), MallBean.ImageData.class);
                bean.price_map = rs.getString("price");
                bean.desc = GsonUtils.fromJsonStr(rs.getString("desc"), new TypeToken<List<String>>() {
                }.getType());
                bean.images = GsonUtils.fromJsonStr(rs.getString("images"), new TypeToken<List<MallBean.ImageData>>() {
                }.getType());

                mallBeanList.add(bean);
            }

            return mallBeanList;
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        } finally {
            IOUtils.close(sr);
        }

        return null;
    }


}
